<?php
/*
 * 缓存SDK,用于设置和更新缓存
 */
namespace App\Http\Service;

use Illuminate\Support\Facades\Cache;

class CacheService
{
    const PLATFORM_KEY_1771 = '1771';
    const PLATFORM_KEY_UOO = 'Uoo';
    const PLATFORM_KEY_5173_VIP = '5173Vip';
    const PREFIX = 'redis_key_';
    const TRADE_TYPE_BUY = 1;
    const TRADE_TYPE_SELL = 2;

    //价格缓存过期时间
    const PRICE_LIST_EXPIRE = 1;

    /*
     * 通过平台名称、大区编号、交易类型(1:购买|2:出售)来得到价格列表的键值,下面相同
     */
    protected function _getPriceListKey($platform, $areaNumber, $tradeType)
    {
        $key = self::PREFIX . 'price_list_' . $platform . '_' . $tradeType . '_ ' . $areaNumber;

        return $key;
    }

    /*
     * 通过平台名称和大区编号及价格类型(buy|sell)从缓存中得到对应大区可售价格和金币列表
     */
    protected function _getPriceList($platform, $areaNumber, $tradeType)
    {
        $key = $this->_getPriceListKey($platform, $areaNumber, $tradeType);

        $res = Cache::get($key, array());
        return $res;
    }

    /*
     * 将对应平台对应大区的金币价格列表存入缓存
      存入缓存的时候已经排序了
     可购买价格列表类似$testPriceList
    $testPriceList = array();
    $res['price'] = 0.02455;//价格是计算过手续费的，比如5173的百分之6和1771/17uoo的百分之一
    $res['unitAmount'] = round(1 / $res['price'], 2);
    $res['goldAmount'] = 12355;
    $res['totalPrice'] = round($res['goldAmount'] / $res['unitAmount'], 2);
    $res['platform'] = '1771';
    $res['credit'] = !isset($v['credit']) ? 999999 : intval($v['credit']);
    $res['storeName'] = '亿达商务';
    $res['buyLink'] = 'www.1771.com/124125.html';
    $res['areaName'] = '广东1区';
    $res['areaNumber'] = 0;
    $res["sendTime"] = 5;
    $res['userId'] = '123141515';
    $testPriceList[] = $res;
    */
    protected function _setPriceList($platform, $areaNumber, $tradeType, $list)
    {
        $key = $this->_getPriceListKey($platform, $areaNumber, $tradeType);

        //存入的数据同时加上时间戳
        $list = array('timestamp' => time(), 'data' => $list);
        Cache::put($key, $list, self::PRICE_LIST_EXPIRE);

        return true;
    }

    /*
     * 获取各平台可购买价格列表
     */
    public function getBuyPriceList1771($areaNumber)
    {
        return $this->_getPriceList(self::PLATFORM_KEY_1771, $areaNumber, self::TRADE_TYPE_BUY);
    }

    public function getBuyPriceList5173Vip($areaNumber)
    {
        return $this->_getPriceList(self::PLATFORM_KEY_5173_VIP, $areaNumber, self::TRADE_TYPE_BUY);
    }

    public function getBuyPriceListUoo($areaNumber)
    {
        return $this->_getPriceList(self::PLATFORM_KEY_UOO, $areaNumber, self::TRADE_TYPE_BUY);
    }

    /*
     * 获取各平台收货格列表
     */
    public function getSellPriceList1771($areaNumber)
    {
        return $this->_getPriceList(self::PLATFORM_KEY_1771, $areaNumber, self::TRADE_TYPE_SELL);
    }

    public function getSellPriceListUoo($areaNumber)
    {
        return $this->_getPriceList(self::PLATFORM_KEY_UOO, $areaNumber, self::TRADE_TYPE_SELL);
    }


    /*
     * 将可购买价格列表存入缓存
     */
    public function setBuyPriceList1771($areaNumber, $list)
    {
        return $this->_setPriceList(self::PLATFORM_KEY_1771, $areaNumber, self::TRADE_TYPE_BUY, $list);
    }

    public function setBuyPriceList5173Vip($areaNumber, $list)
    {
        return $this->_setPriceList(self::PLATFORM_KEY_5173_VIP, $areaNumber, self::TRADE_TYPE_BUY, $list);
    }

    public function setBuyPriceListUoo($areaNumber, $list)
    {
        return $this->_setPriceList(self::PLATFORM_KEY_UOO, $areaNumber, self::TRADE_TYPE_BUY, $list);
    }

    /*
     * 将各平台收货价格列表存入缓存
     *
     */
    public function setSellPriceList1771($areaNumber, $list)
    {
        return $this->_setPriceList(self::PLATFORM_KEY_1771, $areaNumber, self::TRADE_TYPE_SELL, $list);
    }

    public function setSellPriceListUoo($areaNumber, $list)
    {
        return $this->_setPriceList(self::PLATFORM_KEY_UOO, $areaNumber, self::TRADE_TYPE_SELL, $list);
    }

    public function setSyncBuyOrder1771Time()
    {
        $key = 'sync_buy_order_1771';

        return Cache::forever($key, time());
    }

    public function setSyncBuyOrderUooTime()
    {
        $key = 'sync_buy_order_Uoo';

        return Cache::forever($key, time());
    }

    public function setSyncSellOrder1771Time()
    {
        $key = 'sync_sell_order_1771';

        return Cache::forever($key, time());
    }

    public function setSyncSellOrderUooTime()
    {
        $key = 'sync_sell_order_Uoo';

        return Cache::forever($key, time());
    }
}

